package com.minstone.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;


public class CommonBeanUtils extends BeanUtils {
	private static final Logger LOGGER = Logger.getLogger(CommonBeanUtils.class);
	/**
	 * 在map中查找key对应的值
	 * 
	 * */
	private static Object getValue(Map map, String key) {

		if (map.containsKey(key)) {
			return map.get(key);

		} else {

			Iterator iterator = map.keySet().iterator();
			while (iterator.hasNext()) {

				String tempKey = (String) iterator.next();
				Object value = map.get(tempKey);
				if (value instanceof Map) {
					Map te = (Map) value;
					Object tem = getValue(te, key);
					if (tem != null) {
						return tem;

					}
				}

			}

			return null;
		}

	}

	
	public static Object mapToBean(Map map,Class cla) {
		Object obj = null;
		if(cla == null){
			return null;
		}
		
		try {
			obj = cla.newInstance();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		String[] fields ={};
		
		Field[] temp = cla.getDeclaredFields();
		fields = new String[temp.length];
		for(int i =0;i<fields.length;i++){
			fields[i]=temp[i].getName();
		}

		Iterator it = map.keySet().iterator();
		while(it.hasNext()){
			String key = (String) it.next();
			Object value = map.get(key);
			 for(int i =0;i<fields.length;i++){
			    	if(fields[i].equals(key)){
			    		 if(value!=null){
				    	    String methodName = ClassUtil.getMethodName(key, "set");
				    	    try {
					    	    Method method = cla.getMethod(methodName,new Class[]{value.getClass()});
					    	    method.invoke(obj,new Object[]{value});
							} catch (Exception e) {
								LOGGER.error(e.getMessage(), e);
							}
			    		 }
			    	}
			   }
		}
		
		 return obj ;
	}
	
	
	
	
	/**
	 * Map 转换成对象
	 * 
	 * @param map
	 *            数据
	 * @param param
	 *            对应关系
	 * @param cla
	 *            实体模板
	 * */
	public static Object mapToBean(Map map, Map param, Class cla) {
		Object result = new Object();
		if (cla != null) {

			try {
				result = cla.newInstance();
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}

		} else {

			try {
				LOGGER.error("请输入class模板");
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}

		}

		// 改变一中算法迭代map中的数据，首先遍历转换Map param中的key,然后在数据结构map中的寻找值。map中可能是直接一个key对应
		// 一个值,有可能对应一个Map在迭代遍历

		Iterator ite = param.keySet().iterator();
		while (ite.hasNext()) {
			String key = (String) ite.next();
			Object value = getValue(map, key);
			if (value != null) {

				String field = (String) param.get(key);
				setter(result, field, value);
			}

		}
		return result;
	}

	/**
	 * ��ĸ�ֵ
	 * */
	public static void setter(Object obj, String param, Object value) {

		try {
			ClassUtil.setFiledValue(obj, param, value);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}

	}

	/**
	 * ���ȡֵ
	 * */
	public static Object getter(Object obj, String param) {

		Object result = new Object();
		try {
			result = ClassUtil.getFiledValueObject(obj, param);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return result;
	}

	// /**
	// * ��ȡֵ
	// * */
	//
	// public static void setter(Object obj, String param,boolean value){
	// CommonBeanUtils.setter(obj,param,new Boolean(value));
	//
	// }
	//

	public static String getClassName(Object obj) {
		String name = obj.getClass().getName();
		boolean temp = StringUtils.contains(name, ".");
		if (!temp) {
			return name;

		} else {
			return StringUtils.substring(name, name.lastIndexOf(".") + 1,
					name.length());

		}

	}

}